package com.example.sfm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sfm.exception.ObjectException;
import com.example.sfm.pojo.dto.AnnouncementDto;
import com.example.sfm.pojo.dto.AnnouncementQueryDto;
import com.example.sfm.pojo.entity.Announcements;
import com.example.sfm.mapper.AnnouncementsMapper;
import com.example.sfm.pojo.vo.AnnouncementVo;
import com.example.sfm.result.PageResult;
import com.example.sfm.service.IAnnouncementsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sfm.utils.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@Service
public class AnnouncementsServiceImpl extends ServiceImpl<AnnouncementsMapper, Announcements> implements IAnnouncementsService {

    @Override
    public void createAnnouncement(AnnouncementDto announcementDto) {
        Announcements announcement = BeanUtil.copyProperties(announcementDto, Announcements.class);
        announcement.setAuthorId(ThreadLocalUtil.getId());
        if (!save(announcement)) {
            throw new ObjectException("创建公告失败！");
        }
    }

    @Override
    public PageResult<?> listAnnouncements(AnnouncementQueryDto queryDto) {
        String title = queryDto.getTitle();
        String content = queryDto.getContent();
        LambdaQueryWrapper<Announcements> queryWrapper = new LambdaQueryWrapper<Announcements>().like(StringUtils
                .isNotEmpty(title), Announcements::getTitle, title).like(StringUtils.isNotEmpty(content), Announcements::getContent, content);
        Page<Announcements> page = page(new Page<>(queryDto.getPageNum(), queryDto.getPageSize()), queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords().stream().map(announcements -> BeanUtil.copyProperties(announcements, AnnouncementVo.class)).toList());
    }

    @Override
    public void updateAnnouncement(Long id, AnnouncementDto announcementDto) {
        Announcements announcement = getById(id);
        if (announcement == null) {
            throw new ObjectException("公告不存在！");
        }
        BeanUtil.copyProperties(announcementDto, announcement);
        if (!updateById(announcement)) {
            throw new ObjectException("更新公告失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAnnouncements(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ObjectException("公告ID列表不能为空！");
        }
        List<Long> distinctIds = ids.stream().distinct().toList();
        if (!removeBatchByIds(distinctIds)) {
            throw new ObjectException("删除公告失败！");
        }
    }
}
